#!/bin/bash

# 自动安装 restart-java-app 命令补全脚本
# 使用方法: ./install-completion.sh

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_info() {
    echo -e "${BLUE}$1${NC}"
}

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠ $1${NC}"
}

print_error() {
    echo -e "${RED}✗ $1${NC}"
}

# 检测当前shell
detect_shell() {
    if [ -n "$ZSH_VERSION" ]; then
        echo "zsh"
    elif [ -n "$BASH_VERSION" ]; then
        echo "bash"
    else
        echo "unknown"
    fi
}

# 安装bash补全
install_bash_completion() {
    local completion_file="restart-java-app-completion.bash"
    
    if [ ! -f "$completion_file" ]; then
        print_error "找不到补全文件: $completion_file"
        return 1
    fi
    
    # 尝试系统级安装
    if [ -d "/etc/bash_completion.d" ]; then
        if [ -w "/etc/bash_completion.d" ]; then
            # 目录可写，直接复制
            if cp "$completion_file" "/etc/bash_completion.d/restart-java-app" 2>/dev/null; then
                print_success "已安装到系统目录: /etc/bash_completion.d/"
                return 0
            else
                print_warning "复制到系统目录失败，尝试用户目录安装"
            fi
        else
            # 目录不可写，尝试使用sudo
            if sudo cp "$completion_file" "/etc/bash_completion.d/restart-java-app" 2>/dev/null; then
                print_success "已安装到系统目录: /etc/bash_completion.d/"
                return 0
            else
                print_warning "需要管理员权限，尝试用户目录安装"
            fi
        fi
    fi
    
    # 用户级安装
    local user_completion_dir="$HOME/.local/share/bash-completion/completions"
    mkdir -p "$user_completion_dir"
    cp "$completion_file" "$user_completion_dir/restart-java-app"
    print_success "已安装到用户目录: $user_completion_dir/"
    
    # 检查是否需要添加到.bashrc
    if ! grep -q "bash-completion" "$HOME/.bashrc" 2>/dev/null; then
        print_warning "请将以下内容添加到 ~/.bashrc:"
        echo ""
        echo "# Enable bash completion"
        echo "if [ -f ~/.local/share/bash-completion/completions/restart-java-app ]; then"
        echo "    source ~/.local/share/bash-completion/completions/restart-java-app"
        echo "fi"
        echo ""
    fi
}

# 安装zsh补全
install_zsh_completion() {
    local completion_file="restart-java-app-completion.zsh"
    
    if [ ! -f "$completion_file" ]; then
        print_error "找不到补全文件: $completion_file"
        return 1
    fi
    
    # 尝试系统级安装
    if [ -d "/usr/share/zsh/site-functions" ]; then
        if [ -w "/usr/share/zsh/site-functions" ]; then
            # 目录可写，直接复制
            if cp "$completion_file" "/usr/share/zsh/site-functions/_restart-java-app" 2>/dev/null; then
                print_success "已安装到系统目录: /usr/share/zsh/site-functions/"
                return 0
            else
                print_warning "复制到系统目录失败，尝试用户目录安装"
            fi
        else
            # 目录不可写，尝试使用sudo
            if sudo cp "$completion_file" "/usr/share/zsh/site-functions/_restart-java-app" 2>/dev/null; then
                print_success "已安装到系统目录: /usr/share/zsh/site-functions/"
                return 0
            else
                print_warning "需要管理员权限，尝试用户目录安装"
            fi
        fi
    fi
    
    # 用户级安装
    local user_completion_dir="$HOME/.zsh/completions"
    mkdir -p "$user_completion_dir"
    cp "$completion_file" "$user_completion_dir/_restart-java-app"
    print_success "已安装到用户目录: $user_completion_dir/"
    
    # 检查是否需要添加到.zshrc
    if ! grep -q "fpath.*zsh/completions" "$HOME/.zshrc" 2>/dev/null; then
        print_warning "请将以下内容添加到 ~/.zshrc:"
        echo ""
        echo "# Add custom completions to fpath"
        echo "fpath=(~/.zsh/completions \$fpath)"
        echo "autoload -U compinit && compinit"
        echo ""
    fi
}

# 主函数
main() {
    print_info "=== restart-java-app 命令补全安装器 ==="
    echo ""
    
    local current_shell=$(detect_shell)
    print_info "检测到当前shell: $current_shell"
    echo ""
    
    case "$current_shell" in
        "bash")
            print_info "安装bash补全..."
            install_bash_completion
            ;;
        "zsh")
            print_info "安装zsh补全..."
            install_zsh_completion
            ;;
        *)
            print_warning "未知shell类型，请手动选择:"
            echo "1) Bash"
            echo "2) Zsh"
            echo -n "请选择 (1-2): "
            read -r choice
            
            case "$choice" in
                1)
                    install_bash_completion
                    ;;
                2)
                    install_zsh_completion
                    ;;
                *)
                    print_error "无效选择"
                    exit 1
                    ;;
            esac
            ;;
    esac
    
    echo ""
    print_success "补全安装完成！"
    print_info "请重新启动终端或运行以下命令生效:"
    
    case "$current_shell" in
        "bash")
            echo "  source ~/.bashrc"
            ;;
        "zsh")
            echo "  source ~/.zshrc"
            ;;
        *)
            echo "  重新启动终端"
            ;;
    esac
    
    echo ""
    print_info "现在你可以使用 Tab 键补全 restart-java-app 命令了！"
    print_info "示例："
    echo "  restart-java-app <Tab>        # 显示所有可用选项"
    echo "  restart-java-app start <Tab>  # 显示可启动的JAR包"
    echo "  restart-java-app my<Tab>      # 补全以'my'开头的JAR包名"
}

# 运行主函数
main "$@"